package com.yzh.Date;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Date;

/**
 * @author : Bruce
 * @date: 2024/7/16 - 07 - 16 - 16:31
 * @Description: PACKAGE_NAME
 * @version: 1.0
 */
public class TestSimpleDateFormat {
    public static void main(String[] args) throws ParseException {
//        DateFormat df = new DateFormat();
//        public abstract class DateFormat extends Format {
        //抽象类不能实例化 SimpleDateFormat(子类) extends DateFormat（父类是一个抽象类）
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        //String--->Date
        Date parse = df.parse("2019-4-6 12:23:54");
        System.out.println(parse);
        //Date--->String
        String format = df.format(new Date());
        System.out.println(format);

       /* 方式三: 自定义的格式。如: ofPattern( "yyyy-MM-dd hh:mm:ss") ---》重点，以后常用
        DateTimeFormatter df3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        //LocalDateTime-->String:
        LocalDateTime now2 = LocalDateTime.now();
        String format = df3.format(now2);
        System.out.println(format);//2020-06-15 03:22:03
        //String--->LocalDateTime
        TemporalAccessor parse2 = df3.parse("2020-06-15 03:22:03");
        System.out.println(parse2); */

        System.out.println();
        System.out.println();


        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        //LocalDateTime-->String:
        String format1 = dateTimeFormatter.format(LocalDateTime.now());
        System.out.println(format1);

        //String--->LocalDateTime
        TemporalAccessor parse1 = dateTimeFormatter.parse("2020-06-15 03:22:03");
        System.out.println(parse1);

    }
}
